package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelAudioAdapter;
import flc.ast.databinding.ActivitySelAudioBinding;
import flc.ast.databinding.ActivitySelVideoBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.media.MediaLoader;
import stark.common.basic.media.bean.AudioBean;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class SelAudioActivity extends BaseAc<ActivitySelAudioBinding> {

    private SelAudioAdapter selAudioAdapter;
    private int oldPosition = 0;
    private String selPath = "";

    @Override
    protected int onCreate() {
        return R.layout.activity_sel_audio;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSelAudioBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.tvSelAudioComp.setOnClickListener(this);
        mDataBinding.rvAudioList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        selAudioAdapter = new SelAudioAdapter();
        mDataBinding.rvAudioList.setAdapter(selAudioAdapter);
        selAudioAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvSelAudioComp:
                if (TextUtils.isEmpty(selPath)) {
                    ToastUtils.showShort(R.string.please_sel_audio);
                    return;
                }
                AudioEditActivity.audioPath = selPath;
                startActivity(AudioEditActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        selAudioAdapter.getItem(oldPosition).setSelected(false);
        selAudioAdapter.notifyItemChanged(oldPosition);
        oldPosition = position;
        selAudioAdapter.getItem(position).setSelected(true);
        selAudioAdapter.notifyItemChanged(position);
        selPath = selAudioAdapter.getItem(position).getPath();
        mDataBinding.tvSelAudioTip.setText(getString(R.string.sel_video_tip, 1));
        mDataBinding.tvSelAudioComp.setText(getString(R.string.sel_video_comp, 1));
    }

    @Override
    protected void initData() {
        getPermission();
        mDataBinding.tvSelAudioTip.setText(getString(R.string.sel_video_tip, 0));
        mDataBinding.tvSelAudioComp.setText(getString(R.string.sel_video_comp, 0));
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_AUDIO)
                .reqPermissionDesc(getString(R.string.get_music_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        ToastUtils.showShort(R.string.no_permission);
                    }
                }).request();
    }

    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<AudioBean>>() {
            @Override
            public void doBackground(ObservableEmitter<List<AudioBean>> emitter) {
                List<AudioBean> audioBeans = MediaLoader.loadAudio();
                emitter.onNext(audioBeans);
            }

            @Override
            public void accept(List<AudioBean> audioBeanList) {
                if (audioBeanList != null) {
                    selAudioAdapter.setList(audioBeanList);
                    mDataBinding.rvAudioList.setVisibility(View.VISIBLE);
                    mDataBinding.ivSelAudioNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvAudioList.setVisibility(View.GONE);
                    mDataBinding.ivSelAudioNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}